package com.lemon.smartsafetynew.ui.activity.hidden;

import android.support.v4.app.Fragment;
import android.widget.Button;

import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.AppManager;
import com.lemon.smartsafety.app.AppService;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafetynew.ui.activity.base.SwipeBackActivity;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafetynew.entity.event.common.ParamsEvent;
import com.lemon.smartsafetynew.entity.event.common.ViewEvent;
import com.lemon.smartsafetynew.ui.fragment.hidden.SafeHiddenUploadHiddenFragment;
import com.lemon.smartsafetynew.util.constants.KeyConstants;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * @deprecated
 * Created by Andrew on 2017/9/8.
 */

public class SafeHiddenUploadHiddenActivity extends SwipeBackActivity {

    @Bind(R.id.submit_btn)
    Button submitBtn;

    @Override
    protected void initTitle() {
        this.setBarTitle("未上报隐患");
    }

    @Override
    protected void callFinish() {
        AppService.getBus().post(new ViewEvent(SafeHiddenUploadHiddenActivity.class.getName()));
        AppManager.getAppManager().finishActivity(SafeHiddenUploadHiddenActivity.this);
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_safe_hidden_upload_hidden;
    }


    @OnClick(R.id.submit_btn)
    public void onViewClicked() {
        AppService.getBus().post(new ViewEvent(KeyConstants.KEY_SAFE_HIDDEN_UPLOAD_HIDDEN));
    }

    @Override
    protected void initViews() {
        super.initViews();

        submitBtn.setEnabled(false);
        transactionSupportFragment(SafeHiddenUploadHiddenFragment.newInstance());
    }

    private void transactionSupportFragment(Fragment fragment) {
        getSupportFragmentManager().beginTransaction().replace(R.id.content_layout, fragment).commit();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onParamsEvent(ParamsEvent event) {
        if(KeyConstants.KEY_SAFE_HIDDEN_UPLOAD_NUM.equals(event.getTag())) {
            submitBtn.setText("确认（" + event.getParamOne() + "）");
        }

        if("0".equals(event.getParamOne())) {
            submitBtn.setEnabled(false);
        } else {
            submitBtn.setEnabled(true);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent<String> event) {
        if (KeyConstants.URL_REPORT_SAFE_HIDDEN.equals(event.getTag())) {                            //隐患上报
            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {
                mHUDUtil.showHUD();
            } else {
                if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                    mHUDUtil.hidden();
                    if (Constants.Result.SUCCESS == event.getEventResult()) {

                        mHUDUtil.showDoneTip("上报成功", new HUDUtil.CallBack() {
                            @Override
                            public void onCall() {
                                callFinish();
                            }
                        });
                    } else if (Constants.Result.ERROR == event.getEventResult()) {

                        mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                            @Override
                            public void onCall() {

                            }
                        });

                    }
                }
            }
        }
    }

}
